package offer.diff;

/**
 * @author DengYuan2
 * @create 2021-04-19 19:47
 */
public class n_46 {
    public static void main(String[] args) {
        n_46 n = new n_46();
        int i = n.numDecodings("2101");
        System.out.println(i);

    }

    public int numDecodings(String s) {
        if (s==null||s.length()==0){
            return -1;
        }
        if (s.charAt(0)=='0'){
            return 0;
        }
        int len = s.length();
        int[] dp = new int[len+1];
        dp[0] = 1;
        dp[1] = 1;
        for (int i = 1; i < len; i++) {
            if (s.substring(i,i+1).equals("0")){
                int val = s.charAt(i-1)-'0';
                if (val<3&&val>0){
                    dp[i+1]+=dp[i-1];
                }else {
                    return 0;
                }
                continue;
            }else {
                dp[i+1]+=dp[i];
                if (s.charAt(i-1)=='0'){
                    continue;
                }
            }
            int val = Integer.parseInt(s.substring(i - 1, i + 1));
            if (val<=26&&val>0){
                dp[i+1]+=dp[i-1];
            }

        }
        return dp[len];
    }
}
